package com.example.countscore;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RateList2Activity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{

    private static final String TAG = "RateList2Activity";
    Handler handler;
    GridView rateList2;
    ArrayList<Item> listItems;
    MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);

        rateList2 = findViewById(R.id.rateList2);
        rateList2.setOnItemClickListener(this);

        rateList2.setOnItemLongClickListener(this);
//        ProgressBar bar = findViewById(R.id.progressBar);



        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg){
                Log.i(TAG, "handleMessage: 收到消息");

                if(msg.what==9){
                    //接收新网站数据
//                    ArrayList<HashMap<String, String>> listItems = (ArrayList<HashMap<String, String>>) msg.obj;
                    listItems = (ArrayList<Item>) msg.obj;
////                    设置空对象——显示nodata
//                    ArrayList<Item> listItemsNull = new ArrayList<>();
//                    listItems = listItemsNull;

//                    //使用默认适配器
//                    SimpleAdapter listItemAdapter = new SimpleAdapter(
//                            RateList2Activity.this,    //当前activity的对象
//                            listItems,  //listItems数据源
//                            R.layout.list_item, //listItems的XML文件布局
//                            new String[]{"ItemTitle","ItemDetail"},
//                            new int[]{R.id.itemTitle, R.id.itemDetail});
//                    rateList2.setAdapter(listItemAdapter);


                    //使用自定义适配器
                    myAdapter = new MyAdapter(
                            RateList2Activity.this,
                            R.layout.list_item,
                            listItems
                    );
                    rateList2.setAdapter(myAdapter);

                    //没有获得数据时显示
                    rateList2.setEmptyView(findViewById(R.id.nodataText));

                    //切换显示状态
//                    bar.setVisibility(View.GONE);
                    rateList2.setVisibility(View.VISIBLE);

                }

                super.handleMessage(msg);
            }
        };


        //中国银行网站数据
        MyTask_BOC task_boc = new MyTask_BOC();
        task_boc.setHandler(handler);
        Thread t_BOC = new Thread(task_boc);
        t_BOC.start();


    }

//    点击列表中内容，触发函数
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Object itemStPosition = rateList2.getItemAtPosition(position);
//        HashMap<String, String> map = (HashMap<String, String>) itemStPosition;
//        String titleStr = map.get("ItemTitle");
//        String detailStr = map.get("ItemDetail");

        Item item = (Item) itemStPosition;
        String titleStr = item.getCname();
        String detailStr = item.getCval();

        Log.i(TAG, "onItemClick: titleStr:"+titleStr);
        Log.i(TAG, "onItemClick: detailStr:"+detailStr);

//        从控件和从数据map两个里面获得的数据可能不同！
//        TextView title = (TextView) view.findViewById(R.id.itemTitle);
//        TextView detail = (TextView) view.findViewById(R.id.itemDetail);
//        String title2 = String.valueOf(title.getText());
//        String detail2 = String.valueOf(detail.getText());
//        Log.i(TAG, "onItemClick: title2:"+title2);
//        Log.i(TAG, "onItemClick: detail2:"+detail2);

        //打开新窗口
        Intent config = new Intent(this, com.example.countscore.CalculateActivity.class);
        config.putExtra("itemTitle", titleStr);
        config.putExtra("itemDetail", detailStr);
        startActivity(config);

    }


    //长按
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id){
        Log.i(TAG, "onItemLongClick: 长按事件触发");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.i(TAG, "onClick: 对话框事件处理");
                        myAdapter.remove(rateList2.getItemAtPosition(position));
                    }
                }).setNegativeButton("否", null);
        builder.create().show();


        return true;

    }

}